package com.roads.project.service.impl;

import com.roads.common.constant.Constants;
import com.roads.common.constant.UserConstants;
import com.roads.common.core.domain.entity.SysUser;
import com.roads.common.exception.ServiceException;
import com.roads.common.utils.DateUtils;
import com.roads.common.utils.StringUtils;
import com.roads.project.domain.ProInstitution;
import com.roads.project.domain.ProProject;
import com.roads.project.mapper.ProAgencyBoqMapper;
import com.roads.project.mapper.ProAgencyItemsMapper;
import com.roads.project.mapper.ProInstitutionDetailMapper;
import com.roads.project.mapper.ProInstitutionMapper;
import com.roads.project.mapper.ProPaymentItemsMapper;
import com.roads.project.mapper.ProProjectMapper;
import com.roads.project.mapper.ProReportMapper;
import com.roads.project.mapper.ProSupervisorBoqMapper;
import com.roads.project.mapper.ProSupervisorItemsMapper;
import com.roads.project.service.IProProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 建设项目Service业务层处理
 *
 * @author nhp
 * @date 2021-11-15
 */
@Service
public class ProProjectServiceImpl implements IProProjectService {

    @Autowired
    private ProProjectMapper proProjectMapper;

    @Autowired
    private ProPaymentItemsMapper proPaymentItemsMapper;

    @Autowired
    private ProReportMapper proReportMapper;

    @Autowired
    private ProSupervisorBoqMapper proSupervisorBoqMapper;

    @Autowired
    private ProSupervisorItemsMapper proSupervisorItemsMapper;

    @Autowired
    private ProInstitutionMapper institutionMapper;

    @Autowired
    private ProInstitutionDetailMapper institutionDetailMapper;

    @Autowired
    private ProAgencyBoqMapper proAgencyBoqMapper;

    @Autowired
    private ProAgencyItemsMapper proAgencyItemsMapper;

    /**
     * 查询建设项目
     *
     * @param projectId 建设项目主键
     * @return 建设项目
     */
    @Override
    public ProProject selectProProjectByProjectId(Integer projectId) {
        return proProjectMapper.selectProProjectByProjectId(projectId);
    }

    /**
     * 根据项目编号查询建设项目信息
     *
     * @param projectCode 建设项目编号
     * @return 建设项目
     */
    @Override
    public ProProject getProjectByCode(String projectCode) {
        return proProjectMapper.getProjectByCode(projectCode);
    }

    /**
     * 查询建设项目列表
     *
     * @param proProject 建设项目
     * @return 建设项目
     */
    @Override
    public List<ProProject> selectProProjectList(ProProject proProject) {
        return proProjectMapper.selectProProjectList(proProject);
    }

    /**
     * 查询全部建设项目列表
     *
     * @param proProject 建设项目
     * @return 建设项目集合
     */
    @Override
    public List<ProProject> selectProProjectListAll(ProProject proProject) {
        return proProjectMapper.selectProProjectListAll(proProject);
    }

    @Override
    public String checkProjectCodeUnique(ProProject proProject) {
        Integer projectId = StringUtils.isNull(proProject.getProjectId()) ? -1 : proProject.getProjectId();
        ProProject search = proProjectMapper.checkProjectCodeUnique(proProject.getProjectCode());
        if (StringUtils.isNotNull(search) && !search.getProjectId().equals(projectId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增建设项目
     *
     * @param proProject 建设项目
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertProProject(ProProject proProject) {
        proProject.setCreateTime(DateUtils.getNowDate());
        proProjectMapper.insertProProject(proProject);
        // 同步工程支付项目
        proPaymentItemsMapper.importData(proProject.getProjectId());
        // 同步项目报表
        proReportMapper.importData(proProject.getProjectId());
        // 同步项目监理清单
        proSupervisorBoqMapper.sync(proProject.getProjectId());
        // 同步监理支付项目
        proSupervisorItemsMapper.sync(proProject.getProjectId());
        // 同步项目代建清单
        proAgencyBoqMapper.sync(proProject.getProjectId());
        // 同步代建支付项目
        proAgencyItemsMapper.sync(proProject.getProjectId());
        return 1;
    }

    /**
     * 修改建设项目
     *
     * @param proProject 建设项目
     * @return 结果
     */
    @Override
    public int updateProProject(ProProject proProject) {
        proProject.setUpdateTime(DateUtils.getNowDate());
        return proProjectMapper.updateProProject(proProject);
    }

    /**
     * 批量删除建设项目
     *
     * @param projectIds 需要删除的建设项目主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProProjectByProjectIds(Integer[] projectIds) {
        for(Integer projectId : projectIds) {
            ProProject project = selectProProjectByProjectId(projectId);
            if(proProjectMapper.countInstitutionByProjectId(projectId) > 0) {
                throw new ServiceException(String.format("%1$s已添加机构,不能删除", project.getProjectName()));
            }
        }
        // 删除支付项目
        proPaymentItemsMapper.deleteByProjectIds(projectIds);
        // 删除项目报表
        proReportMapper.deleteByProjectIds(projectIds);
        // 删除项目监理清单
        proSupervisorBoqMapper.deleteByProjectIds(projectIds);
        // 删除监理支付项目
        proSupervisorItemsMapper.deleteByProjectIds(projectIds);
        // 删除项目代建清单
        proAgencyBoqMapper.deleteByProjectIds(projectIds);
        // 删除代建支付项目
        proAgencyItemsMapper.deleteByProjectIds(projectIds);
        return proProjectMapper.deleteProProjectByProjectIds(projectIds);
    }

    /**
     * 删除建设项目信息
     *
     * @param projectId 建设项目主键
     * @return 结果
     */
    @Override
    public int deleteProProjectByProjectId(Integer projectId) {
        return proProjectMapper.deleteProProjectByProjectId(projectId);
    }

    /**
     * 查询项目合同金额
     * 需要判断项目下的标段是否有分部，如果有分部，需要处理分部和标段重复问题
     *
     * @param projectId 项目ID
     * @param user 当前登录用户
     * @param isAdmin 是否是管理员
     * @return 合同金额
     */
    @Override
    public BigDecimal getContractTotal(Integer projectId, SysUser user, boolean isAdmin) {
        BigDecimal contractTotal = BigDecimal.valueOf(0);
        if (isAdmin) {
            contractTotal = proProjectMapper.getProjectContractTotal(projectId);
        } else {
            ProInstitution institution = institutionMapper.getModelByInnerCode(user.getInstitutionCode());
            if (StringUtils.isNotNull(institution)) {
                // 如果登录用户是分部或标段，只需要查询分部或标段的合同金额
                if (Constants.INSTITUTION_TYPE_F.equals(institution.getInstitutionType())
                        || Constants.INSTITUTION_TYPE_B.equals(institution.getInstitutionType())) {
                    contractTotal = institutionDetailMapper.getContractTotalByCode(user.getInstitutionCode());
                } else {
                    contractTotal = institutionDetailMapper.getSumBidContractTotalByUser(projectId, user.getUserName());
                }
            } else {
                contractTotal = institutionDetailMapper.getSumBidContractTotalByUser(projectId, user.getUserName());
            }
        }
        return contractTotal;
    }

}
